package com.tudouji.common.utils.poi;

import cn.afterturn.easypoi.cache.HtmlCache;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.ExcelXorHtmlUtil;
import cn.afterturn.easypoi.excel.entity.ExcelToHtmlParams;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.MesQcUploadVo;
import com.tudouji.common.exception.CustomException;
import com.tudouji.common.utils.DateUtils;
import com.tudouji.framework.config.RuoYiConfig;
import com.tudouji.framework.config.TuDouJiConfig;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.project.system.domain.SysDictData;
import com.tudouji.project.system.service.impl.SysDictDataServiceImpl;
import org.apache.poi.ss.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Excel模板导出
 *
 * @author ruoyi
 */
@Component
public class ExcelTempUtil<T>
{
    private static final Logger log = LoggerFactory.getLogger(ExcelTempUtil.class);

    @Autowired
    SysDictDataServiceImpl sysDictDataService;

    private String freightorder_temp="freightorder_temp";
    private String whinoutform_temp="whinoutform";
    private String whoutform_temp="whoutform";
    private String SCBB_temp="SCBB";

    private String getUrl(String url){
        SysDictData dictData = sysDictDataService.getTemplByDictType(url);
        return dictData.getDictValue().replace("/profile", RuoYiConfig.getProfile());
    }

    /**
     * 获取下载路径
     *
     * @param filename 文件名称
     */
    public String getAbsoluteFile(String filename)
    {
        String downloadPath = TuDouJiConfig.getDownloadPath() + filename;
        File desc = new File(downloadPath);
        if (!desc.getParentFile().exists())
        {
            desc.getParentFile().mkdirs();
        }
        return downloadPath;
    }
    /**
     * 编码文件名
     */
    public String encodingFilename(String filename)
    {
        filename = DateUtils.getDate() + "_" + filename + ".xlsx";
        return filename;
    }

    public AjaxResult exportFreightOrder(Object list){
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        TemplateExportParams params = new TemplateExportParams(getUrl(this.freightorder_temp), 0);
        Workbook workbook = ExcelExportUtil.exportExcel(params, map);
        String filename = encodingFilename("货运单");
        try {
            OutputStream out = new FileOutputStream(getAbsoluteFile(filename));
            workbook.write(out);
            return AjaxResult.success(filename);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("导出Excel异常{}", e.getMessage());
            throw new CustomException("导出Excel失败，请联系网站管理员！");
        }
    }

    public AjaxResult exportCommon(List<Object> list, String title){
        String filename = encodingFilename(title);
        ExportParams params = new ExportParams(filename, title);
        params.setStyle(ExcelExportStyler.class);
        Workbook workbook = ExcelExportUtil.exportExcel(params,
                list.get(0).getClass(), list);
        try {
            OutputStream out = new FileOutputStream(getAbsoluteFile(filename));
            workbook.write(out);
            return AjaxResult.success(filename);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("导出Excel异常{}", e.getMessage());
            throw new CustomException("导出Excel失败，请联系网站管理员！");
        }
    }

    public AjaxResult exportWhin(Map<String, Object> map){
        String filename = encodingFilename("入库单");
        TemplateExportParams params = new TemplateExportParams(getUrl(this.whinoutform_temp), 0);
        params.setStyle(ExcelExportStyler.class);
        Workbook workbook = ExcelExportUtil.exportExcel(params,map);
        try {
            OutputStream out = new FileOutputStream(getAbsoluteFile(filename));
            workbook.write(out);
            return AjaxResult.success(filename);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("导出Excel异常{}", e.getMessage());
            throw new CustomException("导出Excel失败，请联系网站管理员！");
        }
    }

    public AjaxResult exportWhinHtml(Map<String, Object> map){
        String filename = encodingFilename("入库单");
        TemplateExportParams params = new TemplateExportParams(getUrl(this.whinoutform_temp), 0);
        params.setStyle(ExcelExportStyler.class);
        Workbook workbook = ExcelExportUtil.exportExcel(params,map);
        try {
            OutputStream out = new FileOutputStream(getAbsoluteFile(filename));
            workbook.write(out);
            return AjaxResult.success(HtmlCache.getHtml(new ExcelToHtmlParams(workbook, false, 0, null)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("导出Excel异常{}", e.getMessage());
            throw new CustomException("导出Excel失败，请联系网站管理员！");
        }
    }

    public AjaxResult exportWhoutSC(Map<String, Object> map){
        String filename = encodingFilename("生产报表");
        TemplateExportParams params = new TemplateExportParams(getUrl(this.SCBB_temp), 0,1);
        params.setStyle(ExcelExportStyler.class);
        Workbook workbook = ExcelExportUtil.exportExcel(params,map);
        try {
            OutputStream out = new FileOutputStream(getAbsoluteFile(filename));
            workbook.write(out);
            return AjaxResult.success(filename);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("导出Excel异常{}", e.getMessage());
            throw new CustomException("导出Excel失败，请联系网站管理员！");
        }
    }

    public AjaxResult exportWhout(Map<String, Object> map){
        String filename = encodingFilename("出库单");
        TemplateExportParams params = new TemplateExportParams(getUrl(this.whoutform_temp), 0);
        params.setStyle(ExcelExportStyler.class);
        Workbook workbook = ExcelExportUtil.exportExcel(params,map);
        try {
            OutputStream out = new FileOutputStream(getAbsoluteFile(filename));
            workbook.write(out);
            return AjaxResult.success(filename);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("导出Excel异常{}", e.getMessage());
            throw new CustomException("导出Excel失败，请联系网站管理员！");
        }
    }

    public AjaxResult exportWhoutHtml(Map<String, Object> map){
        String filename = encodingFilename("出库单");
        TemplateExportParams params = new TemplateExportParams(getUrl(this.whoutform_temp), 0);
        params.setStyle(ExcelExportStyler.class);
        Workbook workbook = ExcelExportUtil.exportExcel(params,map);
        try {
            OutputStream out = new FileOutputStream(getAbsoluteFile(filename));
            workbook.write(out);
            return AjaxResult.success(HtmlCache.getHtml(new ExcelToHtmlParams(workbook, false, 0, null)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("导出Excel异常{}", e.getMessage());
            throw new CustomException("导出Excel失败，请联系网站管理员！");
        }
    }

    public static void main(String[] args) {
        Map<String, Object> map = new HashMap<>();
        String filename = "生产报表";
        TemplateExportParams params = new TemplateExportParams("C:\\Users\\Administrator\\Desktop\\SCBB.xlsx", 0,1);
        params.setStyle(ExcelExportStyler.class);
        Workbook workbook = ExcelExportUtil.exportExcel(params,map);
        try {
            OutputStream out = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\111111111.xlsx");
            workbook.write(out);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("导出Excel异常{}", e.getMessage());
            throw new CustomException("导出Excel失败，请联系网站管理员！");
        }
    }
}
